const app = getApp()
const { processImageUrl } = require('../../../utils/image')
const request = require('../../../utils/request.js');
Page({
  data: {
    course: null,
    isFavorite: false
  },

  onLoad: function(options) {
    const id = options.id
    if (id) {
      this.loadCourseDetail(id)
    }
  },

  // 加载课程详情
  loadCourseDetail: function(id) {
    wx.showLoading({
      title: '加载中...',
    })

    request.request({
      url: `${app.globalData.baseUrl}/api/agricultural-knowledge/residues/${id}`,
      method: 'GET',
      header: {
        'Authorization': `Bearer ${app.getGlobalToken()}`
      },
      success: (res) => {
        if (res.data.code === 200) {
          const course = res.data.data
          // 处理图片URL
          if (course.image) {
            course.image = processImageUrl(course.image)
          }
          
          // 格式化时间
          if (course.updatedAt) {
            course.updatedAt = this.formatDate(course.updatedAt)
          }
          
          this.setData({
            course: course
          })
        } else {
          wx.showToast({
            title: res.data.message || '加载失败',
            icon: 'none'
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none'
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  // 格式化日期
  formatDate: function(dateString) {
    const date = new Date(dateString)
    const year = date.getFullYear()
    const month = (date.getMonth() + 1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    return `${year}-${month}-${day}`
  },

  // 分享
  onShareAppMessage: function() {
    const { course } = this.data
    return {
      title: course.name,
      path: `/pages/school/detail/detail?id=${course.id}`,
      imageUrl: course.image
    }
  }
}) 